<?php

namespace App\Http\Middleware;

use App\Model\AdminTable;
use App\Model\NavTable;
use App\Model\ClassTable;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Str;
use Closure;
use Illuminate\Contracts\Auth\Guard;


class AdminAuth
{
    protected $loginCheck;

    public function __construct(Guard $loginCheck)
    {
        $this->loginCheck = $loginCheck;
    }

    public function handle($request, Closure $next)
    {
        // 登录验证


        //排除登录页面和登录接口
        $pass = ['/adminapi/system/login','/admin/login/index','/admin/index/index'];
        if (in_array(Str::getUrlPath(), $pass)) {
            return $next($request);
        }


        $admin_id= session('admin_id');
        $out_time = session('out_time');
        if (!$out_time or $out_time < time()) {
            if ($request->ajax()) {
                return response('未登录认证', 401);
            } else {
                return redirect()->guest('/admin/login/index');
            }
        }

        $admin_data=AdminTable::ORM()->where('id',$admin_id)->first();
        
        $nav_data=NavTable::getNavList(1);
        $nav_list = $nav_data['data'];//导航列表
        if($admin_data->id == 1){
        	foreach ($nav_list as $v_list) {
		        $id_list=$v_list->id;
		        $count_nav = \DB::select("select count(*) as c from nav as d WHERE status=1 AND id_category={$id_list}")[0]->c;
		        $v_list->count = $count_nav;
		        if($count_nav>0){
		        	//如果没有二级导航链接地址为 javascript
			       	$v_list->url = 'javascript:;';
				}
			}
        	$admin_data->nav=$nav_list;
        }
        if($admin_data->id > 1){
        	$join_info=ClassTable::getClassInfo($admin_data->id_join)['data'];
	        //var_dump($join_info->root);die;
	        $root_id = $join_info->root;//原始权限id
	        $root_list = $root_id;//权限数组
//	        var_dump($root_id);
	        if(!$root_id){
	        	return redirect()->guest('/admin/login/index');
	        }
	        $nav_list_data = [];
	        foreach ($root_list as $v) {
	        	$v_int=(int)$v;//转换数据类型,方便后面的if判断
	        	foreach ($nav_list as $v_list) {
		        	$id_list=$v_list->id;
		        	$count_nav = \DB::select("select count(*) as c from nav as d WHERE status=1 AND id_category={$id_list}")[0]->c;
		        	$v_list->count = $count_nav;
		        	if($count_nav>0){
		        		//如果没有二级导航链接地址为 javascript
			        	$v_list->url = 'javascript:;';
			        }
		        	$nav_li_data = [];
		        	foreach ($v_list->nav_li as $v_li) {
		        		//二级导航权限
		        		$id_li=$v_li->id;
		        		if($v_int==$id_li){
		        			$nav_li_data[] = $v_li;
		        		}
			        }
			        if($v_int==$id_list){
			        	//一级导航权限
		        		$nav_list_data[] = $v_list;
		        	}
				}
			}
	        $admin_data->nav=$nav_list_data;
        }
        
        //当前页面导航
        $page_id = \Request::get('page_id');
		$nav_info=NavTable::getNavInfo($page_id)['data'];
		$admin_data->page_id=$page_id;
		$admin_data->nav_name=$nav_info->name;
		$admin_data->nav_name_category=$nav_info->name_category;
		
		//var_dump('<br>--LoginCheck.php--<br>');
		
		//var_dump($nav_info);
		
		
        //本地开发注释
        foreach ($admin_data as $k=>$v){
        	view()->share($k,$v);
        }
        return $next($request);

    }

}
